package com.mh.web.controller.system;

import com.mh.common.core.controller.BaseController;
import com.mh.common.core.domain.BaseResp;
import com.mh.common.core.page.PageResult;
import com.mh.common.utils.SecurityUtils;
import com.mh.common.utils.UserConstants;
import com.mh.system.domain.SysPost;
import com.mh.system.service.SysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//岗位信息操作处理
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {

    @Autowired
    private SysPostService postService;

    /**
     * 获取岗位列表
     */
    @GetMapping("/list")
    public PageResult list(SysPost post)
    {
        startPage();
        List<SysPost> list = postService.selectPostList(post);
        return getData(list);
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @GetMapping(value = "/{postId}")
    public BaseResp getInfo(@PathVariable Long postId)
    {
        return BaseResp.success(postService.selectPostById(postId));
    }

    /**
     * 新增岗位
     */
    @PostMapping
    public BaseResp add( @RequestBody SysPost post)
    {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
        {
            return BaseResp.error ("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return BaseResp.error ("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setCreateBy(SecurityUtils.getUsername());
        return toAjax(postService.insertPost(post));
    }

    /**
     * 修改岗位
     */
    @PutMapping
    public BaseResp edit(@RequestBody SysPost post)
    {
        if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post)))
        {
            return BaseResp.error ("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post)))
        {
            return BaseResp.error ("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        post.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(postService.updatePost(post));
    }

    /**
     * 删除岗位
     */
    @DeleteMapping("/{postIds}")
    public BaseResp remove(@PathVariable Long[] postIds)
    {
        return toAjax(postService.deletePostByIds(postIds));
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public BaseResp optionselect()
    {
        List<SysPost> posts = postService.selectPostAll();
        return BaseResp.success(posts);
    }

}
